class Solution {
    public int minOperations(int[] nums, int x) {
        int left=0,right=0,sum=0;
        for(int i=0;i<nums.length;i++){
            sum+=nums[i];
        }
        sum-=x;
        if(sum<0) return -1;
        else{
            int ret=0,result=1000000;
            while(right<nums.length){
                ret+=nums[right];
                while(ret>sum){
                    ret-=nums[left++];
                }
                if(ret==sum) result=Math.min(result,nums.length-right+left-1);
                right++;
            }
            return result==1000000?-1:result;
        }
    }
}